//
//  ProduntImageCell.m
//  LazyCat
//
//  Created by Abner on 16/4/28.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "ProduntImageCell.h"
#import "AddProduct.h"
#import "UIButton+AFNetworking.h"
#import "UIImageView+WebCache.h"

@implementation ProduntImageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        self.proimg1 = [[UIImageView alloc]init];
        self.proimg1.userInteractionEnabled = YES;
        [self.contentView addSubview:self.proimg1];
        
        self.proimg2 = [[UIImageView alloc]init];
        self.proimg2.userInteractionEnabled = YES;
        
        [self.contentView addSubview:self.proimg2];
        
        self.proimg3 = [[UIImageView alloc]init];
        self.proimg3.userInteractionEnabled = YES;
        
        [self.contentView addSubview:self.proimg3];
        
        //空白button
        self.proimgbt1 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.proimgbt1.backgroundColor = [UIColor clearColor];
        [self.proimg1 addSubview:self.proimgbt1];
        
//        self.mainImgView = [[UIImageView alloc]init];
//        self.mainImgView.image = [UIImage imageNamed:@"list_main_picture.png"];
//        [self.proimgbt1 addSubview:self.mainImgView];
        
        self.proimgbt2 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.proimgbt2.backgroundColor = [UIColor clearColor];
        [self.proimg2 addSubview:self.proimgbt2];
        
        self.proimgbt3 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.proimgbt3.backgroundColor = [UIColor clearColor];
        [self.proimg3 addSubview:self.proimgbt3];
        
        
        self.deletebt1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deletebt1 setImage:[UIImage imageNamed:@"delect_small_icon.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:self.deletebt1];
        self.deletebt1.tag = 600;
        
        self.deletebt2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deletebt2 setImage:[UIImage imageNamed:@"delect_small_icon.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:self.deletebt2];
        self.deletebt2.tag = 601;
        
        self.deletebt3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deletebt3 setImage:[UIImage imageNamed:@"delect_small_icon.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:self.deletebt3];
        self.deletebt3.tag = 602;
        
        //设置frame
        self.proimg1.frame = CGRectMake(10, 15, 70, 70);
        self.proimg2.frame = CGRectMake(94, 15, 70, 70);
        self.proimg3.frame = CGRectMake(178, 15, 70, 70);
        self.proimgbt1.frame = self.proimg1.bounds;
        self.mainImgView.frame = CGRectMake(0, 49, 70, 21);
        self.proimgbt2.frame = self.proimg2.bounds;
        self.proimgbt3.frame = self.proimg3.bounds;
        self.deletebt1.frame = CGRectMake(70, 6, 19, 19);
        self.deletebt2.frame = CGRectMake(154, 6, 19, 19);
        self.deletebt3.frame = CGRectMake(238, 6, 19, 19);
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 99, ScreenWidth, 1)];
        line.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
        line.alpha = 0.5;
        [self.contentView addSubview:line];
        
        //设置一套默认的显示
        self.proimg1.hidden = NO;
        //        self.proimgbt1.hidden = NO;
        self.mainImgView.hidden = YES;
        self.proimg2.hidden = YES;
        self.proimg3.hidden = YES;
        self.deletebt1.hidden = YES;
        self.deletebt2.hidden = YES;
        self.deletebt3.hidden = YES;
        self.proimg1.image = [UIImage imageNamed:@"upload_image_cell"];
        self.proimgbt1.enabled = YES;
        
        //添加事件
        [self.proimgbt1 addTarget:self action:@selector(proimgbtClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.proimgbt2 addTarget:self action:@selector(proimgbtClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.proimgbt3 addTarget:self action:@selector(proimgbtClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.deletebt1 addTarget:self action:@selector(deletebtClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.deletebt2 addTarget:self action:@selector(deletebtClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.deletebt3 addTarget:self action:@selector(deletebtClicked:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}
#pragma mark --- 添加主图展位图点击事件
-(void)proimgbtClicked
{
    if (self.proimgbtClickedBlock) {
        self.proimgbtClickedBlock(self);
    }
}
-(void)deletebtClicked:(id)sender
{
    UIButton *bt = (UIButton *)sender;
    NSInteger btTag = bt.tag;
    if (self.deletebtClickedBlock) {
        self.deletebtClickedBlock(self,btTag);
    }
}
-(void)setupProduntImageCell:(NSArray *)imgArr aLCProduct:(AddProduct *)aLCProduct
{
    if (imgArr.count == 0) {
        self.proimg1.hidden = NO;
        self.mainImgView.hidden = YES;
        self.proimg2.hidden = YES;
        self.proimg3.hidden = YES;
        self.deletebt1.hidden = YES;
        self.deletebt2.hidden = YES;
        self.deletebt3.hidden = YES;
        self.proimg1.image = [UIImage imageNamed:@"upload_image_cell"];
        self.proimgbt1.enabled = YES;
        
        
    }else if (imgArr.count == 1){
        self.proimg1.layer.masksToBounds = YES;
        self.proimg1.layer.borderColor = SetColorRGBValue(0xeeeeee, 1.0).CGColor;
        self.proimg1.layer.borderWidth = 0.5;
        self.proimg1.hidden = NO;
        self.mainImgView.hidden = NO;
        self.proimg2.hidden = NO;
        self.proimg3.hidden = YES;
        self.deletebt1.hidden = NO;
        self.deletebt2.hidden = YES;
        self.deletebt3.hidden = YES;
        if ([imgArr[0] isKindOfClass:[UIImage class]]) { //判断是从本地要上传的还是网络请求的路径
            self.proimg1.image = imgArr[0];
        }else if (![imgArr[0] isKindOfClass:[UIImage class]]){
            [self.proimg1 sd_setImageWithURL:[NSURL URLWithString:imgArr[0]] placeholderImage:[UIImage imageNamed:@"upload_image_cell"]];
        }
        self.proimg2.image = [UIImage imageNamed:@"upload_image_cell"];
        self.proimgbt1.enabled = NO;
        self.proimgbt2.enabled = YES;
        self.proimgbt3.enabled = YES;
        
    }else if (imgArr.count == 2){
        self.proimg1.layer.masksToBounds = YES;
        self.proimg1.layer.borderColor = SetColorRGBValue(0xeeeeee, 1.0).CGColor;
        self.proimg1.layer.borderWidth = 0.5;
        self.proimg2.layer.masksToBounds = YES;
        self.proimg2.layer.borderColor = SetColorRGBValue(0xeeeeee, 1.0).CGColor;
        self.proimg2.layer.borderWidth = 0.5;
        self.proimg1.hidden = NO;
        self.mainImgView.hidden = NO;
        self.proimg2.hidden = NO;
        self.proimg3.hidden = NO;
        self.deletebt1.hidden = NO;
        self.deletebt2.hidden = NO;
        self.deletebt3.hidden = YES;
        if ([imgArr[0] isKindOfClass:[UIImage class]]) { //判断是从本地要上传的还是网络请求的路径
            
            self.proimg1.image = imgArr[0];
        }else if (![imgArr[0] isKindOfClass:[UIImage class]]){
            [self.proimg1 sd_setImageWithURL:[NSURL URLWithString:imgArr[0]] placeholderImage:[UIImage imageNamed:@"upload_image_cell"]];
        }
        if ([imgArr[1] isKindOfClass:[UIImage class]]) { //判断是从本地要上传的还是网络请求的路径
            self.proimg2.image = imgArr[1];
        }else if (![imgArr[1] isKindOfClass:[UIImage class]]){
            [self.proimg2 sd_setImageWithURL:[NSURL URLWithString:imgArr[1]] placeholderImage:[UIImage imageNamed:@"upload_image_cell"]];
        }
        self.proimg3.image = [UIImage imageNamed:@"upload_image_cell"];
        self.proimgbt1.enabled = NO;
        self.proimgbt2.enabled = NO;
        self.proimgbt3.enabled = YES;
        
        
    }else if (imgArr.count >= 3){
        self.proimg1.layer.masksToBounds = YES;
        self.proimg1.layer.borderColor = SetColorRGBValue(0xeeeeee, 1.0).CGColor;
        self.proimg1.layer.borderWidth = 0.5;
        self.proimg2.layer.masksToBounds = YES;
        self.proimg2.layer.borderColor = SetColorRGBValue(0xeeeeee, 1.0).CGColor;
        self.proimg2.layer.borderWidth = 0.5;
        self.proimg3.layer.masksToBounds = YES;
        self.proimg3.layer.borderColor = SetColorRGBValue(0xeeeeee, 1.0).CGColor;
        self.proimg3.layer.borderWidth = 0.5;
        
        self.proimg1.hidden = NO;
        self.mainImgView.hidden = NO;
        self.proimg2.hidden = NO;
        self.proimg3.hidden = NO;
        self.deletebt1.hidden = NO;
        self.deletebt2.hidden = NO;
        self.deletebt3.hidden = NO;
        if ([imgArr[0] isKindOfClass:[UIImage class]]) { //判断是从本地要上传的还是网络请求的路径
            self.proimg1.image = imgArr[0];
        }else if (![imgArr[0] isKindOfClass:[UIImage class]]){
            [self.proimg1 sd_setImageWithURL:[NSURL URLWithString:imgArr[0]] placeholderImage:[UIImage imageNamed:@"upload_image_cell"]];
        }
        if ([imgArr[1] isKindOfClass:[UIImage class]]) { //判断是从本地要上传的还是网络请求的路径
            self.proimg2.image = imgArr[1];
        }else if (![imgArr[1] isKindOfClass:[UIImage class]]){
            [self.proimg2 sd_setImageWithURL:[NSURL URLWithString:imgArr[1]] placeholderImage:[UIImage imageNamed:@"upload_image_cell"]];
        }
        if ([imgArr[2] isKindOfClass:[UIImage class]]) { //判断是从本地要上传的还是网络请求的路径
            self.proimg3.image = imgArr[2];
        }else if (![imgArr[2] isKindOfClass:[UIImage class]]){
            [self.proimg3 sd_setImageWithURL:[NSURL URLWithString:imgArr[2]] placeholderImage:[UIImage imageNamed:@"upload_image_cell"]];
        }
        self.proimgbt1.enabled = NO;
        self.proimgbt2.enabled = NO;
        self.proimgbt3.enabled = NO;
    }
}
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProduntImageCellIdentifier";
    ProduntImageCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProduntImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
